#include <bits/stdc++.h>
using namespace std;

string str, na, fa;
char tag;
map<string, string> mp;
string find(string son)
{
    if (mp[son] == son)
        return son;
    mp[son] = find(mp[son]);
    return mp[son];
}

int main()
{
    while (true)
    {
        cin >> str;
        if (str == "$")
            break;
        tag = str[0];
        na = str.substr(1);
        if (tag == '#')
        {
            fa = na;
            if (!mp.count(fa))
            {
                mp[na] = na;
            }
        }
        else if (tag == '+')
        {
            mp[na] = find(fa);
        }
        else
        {
            cout << na << " " << find(na) << endl;
        }
    }
    return 0;
}
